package com.localjoycommunity.cpdj.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.localjoycommunity.cpdj.project.system.domain.TwoNewOrganization;
import com.localjoycommunity.cpdj.project.system.domain.User;
import com.localjoycommunity.cpdj.project.system.domain.UserCompany;
import com.localjoycommunity.cpdj.project.system.mapper.UserCompanyMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserMapper;
import com.localjoycommunity.cpdj.project.system.service.ITwoNewOrganizationService;
import com.localjoycommunity.cpdj.project.system.service.IUserCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lida
 * @Date: 2020/11/14 0014 上午 11:35
 * @Description:
 */
@Service
public class UserCompanyServiceImpl extends ServiceImpl<UserCompanyMapper, UserCompany> implements IUserCompanyService {

    @Autowired
    private ITwoNewOrganizationService twoNewOrganizationService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户收藏两新组织
     *
     * @param userCompany
     */
    @Override
    public int twoNewDig(UserCompany userCompany) {
        // 1. 先查询
        UserCompany one = baseMapper.selectOne(new QueryWrapper<UserCompany>().lambda().eq(UserCompany::getUserId, userCompany.getUserId())
                .eq(UserCompany::getTwoNewId, userCompany.getTwoNewId()));
        if (one == null) {
            userCompany.setIsDig(1);
            twoNewOrganizationService.updateDigNum(userCompany.getTwoNewId(), 1);
            return baseMapper.insert(userCompany);
        }
        User user = userMapper.selectUserById(userCompany.getUserId());
        Long collectNum = user.getCollectNum() == null ? 0 : user.getCollectNum();
        one.setIsDig(one.getIsDig() ^ 1);
        if (one.getIsDig() == 1) {
            twoNewOrganizationService.updateDigNum(userCompany.getTwoNewId(), 1);
            user.setCollectNum(collectNum + 1);
        } else {
            twoNewOrganizationService.updateDigNum(userCompany.getTwoNewId(), -1);
            collectNum = collectNum - 1 <= 0 ? 0 : collectNum - 1;
            user.setCollectNum(collectNum);
        }
        userMapper.updateUser(user);
        return baseMapper.updateById(one);
    }

    /**
     * 用户收藏列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<TwoNewOrganization> selectUserCollect(Integer userId) {
        List<UserCompany> userCompanies = baseMapper.selectList(new QueryWrapper<UserCompany>().lambda().eq(UserCompany::getUserId, userId).eq(UserCompany::getIsDig, 1));
        if (userCompanies == null || userCompanies.size() == 0) {
            return null;
        }
        List<TwoNewOrganization> list = new ArrayList<>();
        for (UserCompany userCompany : userCompanies) {
            TwoNewOrganization twoNewOrganization = twoNewOrganizationService.selectTwoNewOrganizationById(userCompany.getTwoNewId());
            list.add(twoNewOrganization);
        }
        return list;
    }

    @Override
    public Integer isDig(Integer userId, Long twoNewId) {
        return baseMapper.isDig(userId, twoNewId).orElse(0);
    }
}
